package cn.zl.model;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjUtil;
import cn.yunbamboos.Model;
import cn.yunbamboos.mybatis.annotation.TableField;
import cn.yunbamboos.mybatis.annotation.TableName;
import cn.yunbamboos.mybatis.annotation.TablePrimaryKey;
import com.alibaba.fastjson2.JSONObject;

import java.time.LocalDateTime;

@TableName("zl_stream_push")
public class StreamPush implements Model {

    @TablePrimaryKey(value = "push_id")
    private String pushId;
    @TableField(value = "app")
    private String app;
    @TableField(value = "stream")
    private String stream;
    @TableField(value = "state")
    private int state;
    @TableField(value = "update_time")
    private LocalDateTime updateTime;
    @TableField(value = "create_time")
    private LocalDateTime createTime;

    @Override
    public JSONObject encode() {
        JSONObject json = new JSONObject();
        json.put("push_id", pushId);
        json.put("app", app);
        json.put("stream", stream);
        if(ObjUtil.isNotNull(this.updateTime)){
            json.put("update_time", LocalDateTimeUtil.format(updateTime, DatePattern.NORM_DATETIME_PATTERN));
        }
        if(ObjUtil.isNotNull(this.createTime)){
            json.put("create_time", LocalDateTimeUtil.format(createTime, DatePattern.NORM_DATETIME_PATTERN));
        }
        json.put("state", state);
        return json;
    }

    @Override
    public void decode(JSONObject json) {
        if (json.containsKey("push_id")) this.pushId = json.getString("push_id");
        if (json.containsKey("app")) this.app = json.getString("app");
        if (json.containsKey("stream")) this.stream = json.getString("stream");
        if (json.containsKey("state")) this.state = json.getInteger("state");
    }

    public String getPushId() {
        return pushId;
    }

    public void setPushId(String pushId) {
        this.pushId = pushId;
    }

    public String getApp() {
        return app;
    }

    public void setApp(String app) {
        this.app = app;
    }

    public String getStream() {
        return stream;
    }

    public void setStream(String stream) {
        this.stream = stream;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }
}
